package com.ttl.bc.dwr.service;

import java.util.ArrayList;


import com.ttl.bc.ic.item.BCItemBean;
import com.ttl.bc.ic.item.BCItemDAO;
import com.ttl.bc.ic.stklocation.BCStkLocationBean;
import com.ttl.bc.ic.stklocation.BCStkLocationDAO;
import com.ttl.bc.master.itemunit.BCItemUnitBean;
import com.ttl.bc.master.itemunit.BCItemUnitDAO;
import com.ttl.bc.master.shelf.BCShelfBean;
import com.ttl.bc.master.shelf.BCShelfDAO;



public class ItemLocationService {
	
	@SuppressWarnings("unchecked")
	public ItemLocationBean getData(String id) {

		ItemLocationBean iloc = new ItemLocationBean();
		iloc.setCode(id.toUpperCase());
		
		if(id.equals("") || id==null) {
//			System.out.println("Class ItemLocationService ->id: empty string or null");
			return iloc;			
		}
		
		BCItemBean bean = new BCItemBean();
		//ItemBean bean = new ItemBean();
		BCStkLocationBean bean2 = new BCStkLocationBean(); 
		BCShelfBean bean3 = new BCShelfBean();
				
		BCItemDAO dao = new BCItemDAO();
		ArrayList al = (ArrayList)dao.find(id);
				
		if (al.size()>0) {
			bean = (BCItemBean)al.get(0);
			//bean = (ItemBean)al.get(0);			
			bean.setCode(bean.getCode().toUpperCase());
			bean.setName1(bean.getName1().toUpperCase());		
									
			iloc.setName1(bean.getName1());
			iloc.setShortName(bean.getShortName());
			System.out.println("DDDDDDDDDDDDDDDDDDDD"+iloc.getShortName());
			iloc.setIttl_row(bean.getIttl_row());
			iloc.setIttl_shelf(bean.getIttl_shelf());
			
		}else {			
			iloc.setName1("");
			iloc.setShortName("");
			iloc.setIttl_row("");
			iloc.setIttl_shelf("");			
		}				
		
		String hql = "FROM BCStkLocationBean p WHERE upper(p.itemCode) =  '"+id.toUpperCase()+"'";
		//System.out.println(hql);
		BCStkLocationDAO dao2 = new BCStkLocationDAO();				
		ArrayList al2 = (ArrayList)dao2.find(hql);
		ArrayList alShelfCode = new ArrayList();			
		ArrayList alShelfName = new ArrayList();

		if (al2.size()>0) {
			
			for(int i=0; i<al2.size(); i++) {
				bean2 = (BCStkLocationBean)al2.get(i);
				iloc.setWhCode(bean2.getWhCode());		
								
				alShelfCode.add(bean2.getShelfCode());
				//System.out.println("bean2.getShelf() = "+bean2.getShelf());
				
				String hql2 = "FROM BCShelfBean p WHERE upper(p.code) =  '"+bean2.getShelfCode()+"'";				
				BCShelfDAO dao3 = new BCShelfDAO();				
				ArrayList al3= (ArrayList)dao3.find(hql2);				
				
				if (al3.size()==1) {
					bean3 = (BCShelfBean)al3.get(0);
					alShelfName.add(bean3.getName());					
				}			
			}
			
			String hql3 = "FROM BCItemUnitBean p WHERE upper(p.code) =  '"+bean2.getUnitCode()+"'";

			BCItemUnitDAO dao3 = new BCItemUnitDAO();				
			ArrayList al4 = (ArrayList)dao3.find(hql3);			
			
			if (al4.size()==1) {
				BCItemUnitBean bean4 = (BCItemUnitBean)al4.get(0);
				iloc.setUnitName(bean4.getName());								
			}
			
			iloc.setShelfCode(alShelfCode);
			iloc.setShelfName(alShelfName);
			iloc.setUnitCode(bean2.getUnitCode());
			//System.out.println("iloc.getShelfCode().toArray() = "+iloc.getShelfCode().toArray());
			//System.out.println("iloc.getShelfName().toArray() = "+iloc.getShelfName().toArray());
		}else {
//			iloc.setWhCode("");
//			iloc.setShelfCode("");			
		}
		
		return iloc;//bean;
	}
	

}
